<!-- 专家列表 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键字" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="userType === 2" label="专家称号" prop="expertTitle">
        <el-select
          v-model="queryParams.expertTitle"
          placeholder="请选择专家称号"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.sys_expert_title"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <template v-if="pageType === 'page' && userType !== 2">
        <el-form-item label="单位" prop="comId">
          <SelectCompany
            :defaultValue="queryParams.comId"
            :companyType="0"
            @change="(e) => upDataSearch('comId', e)"
          />
        </el-form-item>
        <el-form-item label="部门" prop="deptname">
          <SelectDept
            :defaultValue="queryParams.deptname"
            valueKey="deptName"
            :comId="queryParams.comId"
            @change="(e) => upDataSearch('deptname', e)"
          />
        </el-form-item>
        <el-form-item label="专家职称" prop="postId">
          <el-select
            v-model="queryParams.postId"
            placeholder="请选择专家职称"
            clearable
            @change="handleQuery"
          >
            <el-option
              v-for="item in postOptions"
              :key="item.id"
              :label="item.postName"
              :value="item.postId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="apStatus">
          <el-select
            v-model="queryParams.apStatus"
            placeholder="请选择审核状态"
            clearable
            @change="handleQuery"
          >
            <el-option
              v-for="dict in dict.type.ap_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="注册方式" prop="registerType">
          <el-select
            v-model="queryParams.registerType"
            placeholder="请选择注册方式"
            clearable
            @change="handleQuery"
          >
            <el-option
              v-for="dict in dict.type.expert_registration_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="createTime"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleQuery"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账号状态" prop="disabled">
          <el-select
            v-model="queryParams.disabled"
            placeholder="请选择账号状态"
            clearable
            @change="handleQuery"
          >
            <el-option label="启用" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
        </el-form-item>
      </template>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row v-if="pageType === 'page'" :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['expert:expert:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['expert:expert:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['expert:expert:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col v-if="userType !== 2" :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['expert:expert:import']"
          >导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="expertList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :fixed="pageType === 'page' ? 'left' : false"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="userName"
        width="100"
        :fixed="pageType === 'page' ? 'left' : false"
      />
      <el-table-column label="性别" align="center" prop="sex" width="60">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="headimg" width="100">
        <template slot-scope="scope">
          <image-preview
            v-if="scope.row.headimg"
            :src="scope.row.headimg"
            :width="pageType === 'page' ? 50 : 25"
            :height="pageType === 'page' ? 50 : 25"
          />
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="mobile" width="120" />
      <el-table-column label="单位" align="center" prop="com" min-width="250" />
      <template v-if="pageType === 'page'">
        <el-table-column
          label="部门"
          align="center"
          prop="deptname"
          width="120"
        />
        <el-table-column
          label="专家称号"
          align="center"
          width="100"
        >
          <template slot-scope="{ row }">
            <dict-tag
                :options="dict.type.sys_expert_title"
                :value="row.expertTitle"
              />
          </template>
        </el-table-column>
        <el-table-column
          label="专家职称"
          align="center"
          prop="postName"
          width="100"
        >
        </el-table-column>
        <template v-if="userType !== 2">
          <el-table-column
            label="注册方式"
            align="center"
            prop="registerType"
            width="100"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.expert_registration_method"
                :value="scope.row.registerType"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="云豆余额"
            align="center"
            prop="balance"
            width="100"
          >
            <template slot-scope="{ row }">
              <div class="cup" @dblclick="recharge(row)">{{ row.balance }}</div>
            </template>
          </el-table-column>
          <el-table-column
            label="患者数量(人)"
            align="center"
            prop="usernum"
            width="100"
          />
          <el-table-column
            label="专家好友数量(人)"
            align="center"
            prop="friendsnum"
            width="120"
          />
          <el-table-column
            label="视频咨询总数(次)"
            align="center"
            prop="cnum"
            width="120"
          />
          <el-table-column
            label="已开通服务"
            align="center"
            class-name="small-padding fixed-width"
            width="120"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.setting">
                {{
                  Number(scope.row.setting.openImmediately) +
                  Number(scope.row.setting.openLive) +
                  Number(scope.row.setting.openSubscribe)
                }}项
              </span>
              <el-button
                size="mini"
                type="text"
                @click="handleUpdateSet(scope.row)"
                >查看</el-button
              >
            </template>
          </el-table-column>

          <el-table-column
            label="当前在线状态"
            align="center"
            prop="currentOnlineStatus"
            width="120"
          />
          <el-table-column
            label="当前在线设备"
            align="center"
            prop="currentOnlineEquipment"
            width="120"
          />
        </template>
        <el-table-column
          label="账号状态"
          align="center"
          width="90"
        >
          <template slot-scope="{ row }">
            <el-tag v-if="row.disabled === 0">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="更新时间"
          align="center"
          prop="updateTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" width="90">
          <template slot-scope="{ row }">
            <dict-tag
              :options="dict.type.ap_status"
              :value="row.apStatus"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="120px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['expert:expert:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['expert:expert:remove']"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.userType === 2"
              size="mini"
              type="text"
              icon="el-icon-connection"
              @click="openDialectical(scope.row)"
              >关联辩证</el-button
            >
          </template>
        </el-table-column>
      </template>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="titleSet"
      :visible.sync="openSet"
      width="600px"
      append-to-body
    >
      <el-form ref="formSet" :model="formSet" label-width="100px">
        <el-row>
          <el-col :span="24">
            <div class="mb-22 ml-18 fw-6">
              {{ formSet.userName }}，已开通服务
              {{
                Number(formSet.setting.openImmediately) +
                Number(formSet.setting.openLive) +
                Number(formSet.setting.openSubscribe)
              }}项 ，未开通服务
              {{
                Number(3) -
                (Number(formSet.setting.openImmediately) +
                  Number(formSet.setting.openLive) +
                  Number(formSet.setting.openSubscribe))
              }}
              项
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="预约咨询：">
              <el-switch
                v-model="formSet.setting.openImmediately"
                active-text="开启"
                active-value="1"
                inactive-value="0"
                inactive-text="关闭"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="即时咨询：">
              <el-switch
                v-model="formSet.setting.openSubscribe"
                active-text="开启"
                active-value="1"
                inactive-value="0"
                inactive-text="关闭"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="在线直播：">
              <el-switch
                v-model="formSet.setting.openLive"
                active-text="开启"
                active-value="1"
                inactive-value="0"
                inactive-text="关闭"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormSet">确 定</el-button>
        <el-button @click="cancelSet">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改专家对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <ExpertDetails ref="ExpertDetails" :userType="userType" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 专家导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="
          upload.url +
          '?updateSupport=' +
          upload.updateSupport +
          '&comId=' +
          upload.comId
        "
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span class="pr-10 fz-14">单位：</span>
          <SelectCompany
            :defaultValue="upload.comId"
            :companyType="0"
            @change="(e) => (upload.comId = e)"
          />

          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />
            是否更新已经存在的专家数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
          <br /><span style="color: red">最大支持500行数据</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <RechargeModal ref="RechargeModal" @refresh="getList" />
    <Dialectical ref="Dialectical"  @refresh="getList" />
  </div>
</template>

<script>
import {
  listExpert,
  getExpert,
  delExpert,
  addExpert,
  updateExpert,
  updateExpertSet,
} from "@/api/expert/expert";
import { optionselect } from "@/api/expert/post";
import { getToken } from "@/utils/auth";
import SelectCompany from "@/components/SelectCompany";
import SelectDept from "@/components/SelectDept";
import ImageUpload from "@/components/ImageUpload";
import RechargeModal from "@/components/RechargeModal";
import ExpertDetails from "./expertDetails";
import Dialectical from '../platform/dialectical'
import { getBaseUrl } from "@/utils/ruoyi.js";

export default {
  name: "Expert",
  dicts: ["ap_status", "expert_registration_method", "sys_user_sex", "sys_expert_title"],
  components: {
    SelectCompany,
    SelectDept,
    ImageUpload,
    RechargeModal,
    ExpertDetails,
    Dialectical
  },
  props: {
    // 页面类型, page || dialog
    pageType: {
      type: String,
      default: "page",
    },
    // 专家类型, 平台专家: 2
    userType: {
      type: Number
    }
  },
  data() {
    return {
      // 用户导入参数
      upload: {
        comId: null,
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: getBaseUrl() + "/expert/expert/importData",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 岗位选项
      postOptions: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家表格数据
      expertList: [],
      // 弹出层标题
      title: "",
      // 弹出层标题
      titleSet: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openSet: false,
      createTime: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: null,
        comId: null,
        deptname: null,
        apStatus: null,
        registerType: null,
        createTimeStart: null,
        createTimeEnd: null,
        userType: undefined,
        expertTitle: undefined,
        disabled: 0
      },
      // 表单参数
      formSet: {
        id: undefined,
        userName: "",
        setting: {
          openImmediately: 0,
          openLive: 0,
          openSubscribe: 0,
        },
      },
    };
  },
  created() {
    this.getList();
    this.getOptionselect();
  },
  activated() {
    this.getList();
    this.getOptionselect();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "专家导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "expert/expert/importTemplate",
        {},
        `批量导入专家模板.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.comId = null;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      if (this.upload.comId) {
        this.$refs.upload.submit();
      } else {
        this.$message({
          showClose: true,
          message: "请选择单位",
          type: "error",
        });
        return;
      }
    },
    upDataSearch(key, value) {
      this.queryParams[key] = value;
      this.handleQuery();
    },
    getOptionselect() {
      optionselect({ pageNum: 1, pageSize: 999 }).then((response) => {
        this.postOptions = response.data;
      });
    },
    /** 查询专家列表 */
    getList() {
      this.queryParams.createTimeStart =
        this.createTime && this.createTime.length ? this.createTime[0] : "";
      this.queryParams.createTimeEnd =
        this.createTime && this.createTime.length ? this.createTime[1] : "";
      this.queryParams.userType = this.userType;
      this.loading = true;
      listExpert(this.queryParams).then((response) => {
        this.expertList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancelSet() {
      this.openSet = false;
      this.reset();
    },
    // 表单重置
    reset() {
      Object.assign(this.$data.formSet, this.$options.data().formSet);
      this.resetForm("formSet");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      const that = this;
      that.title = "添加专家";
      that.open = true;
      that.$nextTick(() => {
        that.$refs.ExpertDetails.setForm();
      });
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      const that = this;
      const id = row.id || that.ids;
      that.title = "修改专家";
      that.open = true;

      const { data } = await getExpert(id);
      that.$nextTick(() => {
        that.$refs.ExpertDetails.setForm(data);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.ExpertDetails.validateForm().then((form) => {
        if (form.id) {
          updateExpert(form).then(() => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          addExpert(form).then(() => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    /** 修改按钮操作 */
    handleUpdateSet(row) {
      this.reset();
      this.titleSet = "服务开通";
      this.openSet = true;
      this.formSet = row;
    },
    /** 提交按钮 */
    submitFormSet() {
      updateExpertSet(this.formSet.setting).then((response) => {
        this.$modal.msgSuccess("保存成功");
        this.openSet = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除专家编号为"' + ids + '"的数据项？')
        .then(function () {
          return delExpert(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    // 双击充值
    recharge(row) {
      this.$refs.RechargeModal.show({ expId: row.id });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "expert/expert/export",
        {
          ...this.queryParams,
        },
        `专家列表_${this.parseTime(new Date(), "{y}-{m}-{d}")}.xlsx`
      );
    },
    openDialectical(row) {
      this.$refs.Dialectical.openModal(row.id);
    }
  },
};
</script>
<style scoped></style>
